package semspe;
import javax.swing.*;
import java.awt.event.*;

class AjoutBouteille extends JFrame implements ActionListener//implements WindowListener
{
	private JPanel panel;
	private JLabel marque;
	private JLabel volAlcool;
	private JLabel volBouteille;
	private JLabel type;
	private JLabel quantite;
	private JLabel stock;
	private JLabel com;
        private JLabel pourcent;
        private JLabel litre;
	
	private JTextField txtMarque;
	private JTextField txtVolAlcool;
	private JTextField txtVolBouteille;
	private JComboBox txtType;
	private JTextField txtQuantite;
	private JComboBox txtStock;
	private JTextArea txtCom;
	
	private JOptionPane jop1;
	private JButton OK;
	private JButton annuler;
	
	public AjoutBouteille()
	{
		super("Ajouter une bouteille");
		this.setSize(400,600);
		this.setResizable(false);
		this.setVisible(true);
		
		this.getContentPane().setLayout(null);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		this.panel = new JPanel();
		//MARQUE BOUTEILLE
		this.marque = new JLabel("Marque :");
		this.getContentPane().add(this.marque);
		this.marque.setBounds(30,20,150,20);
		this.txtMarque = new JTextField("");
		this.getContentPane().add(this.txtMarque);
		this.txtMarque.setBounds(200,20,150,20);
		
		//VOLUME ALCOOL
		this.volAlcool = new JLabel("Volume d'alcool :");
		this.getContentPane().add(this.volAlcool);
		this.volAlcool.setBounds(30,60,150,20);
		this.txtVolAlcool = new JTextField("");
		this.getContentPane().add(this.txtVolAlcool);
		this.txtVolAlcool.setBounds(200,60,50,20);
                this.pourcent = new JLabel("%");
		this.getContentPane().add(this.pourcent);
		this.pourcent.setBounds(251,60,20,20);
		
		//VOLUME BOUTEILLE
		this.volBouteille = new JLabel("Volume bouteille :");
		this.getContentPane().add(this.volBouteille);
		this.volBouteille.setBounds(30,100,150,20);
		this.txtVolBouteille = new JTextField("");
		this.getContentPane().add(this.txtVolBouteille);
		this.txtVolBouteille.setBounds(200,100,50,20);
                this.litre = new JLabel("L");
		this.getContentPane().add(this.litre);
		this.litre.setBounds(251,100,20,20);
		
				
		//Type
		this.type = new JLabel("Type :");
		this.getContentPane().add(this.type);
		this.type.setBounds(30,140,150,20);
		this.txtType = new JComboBox();
		this.getContentPane().add(this.txtType);
		this.txtType.setBounds(200,140,150,20);
                TraitRequete ligneType = new TraitRequete();
                String [] resultatVouluLigneType = new String[]{"Count(libelle)"};
                String [][] resRecupLigneType = ligneType.ExecuteRequete("SELECT Count(libelle) FROM typealcool", resultatVouluLigneType ,1);
                TraitRequete recupTypes = new TraitRequete();
                String [] resultatVouluType = new String[]{"libelle"};
                String [][] resRecupTypes = recupTypes.ExecuteRequete("SELECT libelle FROM typealcool;", resultatVouluType, Integer.parseInt(resRecupLigneType[0][0]));
                int i=0;
                for(i=0; i<resRecupTypes[0].length;i++)
                {
                  txtType.addItem(resRecupTypes[0][i]);  
                }
		
		//Quantité
		this.quantite = new JLabel("Quantité :");
		this.getContentPane().add(this.quantite);
		this.quantite.setBounds(30,180,150,20);
		this.txtQuantite = new JTextField("");
		this.getContentPane().add(this.txtQuantite);
		this.txtQuantite.setBounds(200,180,50,20);
                this.litre = new JLabel("bouteille(s)");
		this.getContentPane().add(this.litre);
		this.litre.setBounds(251,180,100,20);
		
		//Stock
		this.stock = new JLabel("Stock :");
		this.getContentPane().add(this.stock);
		this.stock.setBounds(30,220,150,20);
		this.txtStock = new JComboBox();
		this.getContentPane().add(this.txtStock);
		this.txtStock.setBounds(200,220,150,20);
                TraitRequete ligneStock = new TraitRequete();
                String [] resultatVouluLigneStock = new String[]{"Count(libelleStock)"};
                String [][] resRecupLigneStock = ligneType.ExecuteRequete("SELECT Count(libelleStock) FROM stock", resultatVouluLigneStock,1 );
                TraitRequete recupStocks = new TraitRequete();
                String [] resultatVouluStock = new String[]{"libelleStock"};
                String [][] resRecupStocks = recupTypes.ExecuteRequete("SELECT libelleStock FROM stock;", resultatVouluStock, Integer.parseInt(resRecupLigneStock[0][0]));
                for(i=0; i<resRecupStocks[0].length;i++)
                {
                  txtStock.addItem(resRecupStocks[0][i]);  
                }
		
		//COMMENTAIRES
		this.com = new JLabel("Commentaires :");
		this.getContentPane().add(this.com);
		this.com.setBounds(30,260,150,20);
		this.txtCom = new JTextArea("");
		this.getContentPane().add(this.txtCom);
		this.txtCom.setBounds(200,260,150,100);
		
		//BUT
		this.OK = new JButton("Ajouter");
		this.getContentPane().add(this.OK);
		this.OK.setBounds(100,400,100,20);
		this.annuler = new JButton("Annuler");
		this.getContentPane().add(this.annuler);
		this.annuler.setBounds(250,400,100,20);
		
		this.annuler.addActionListener(this);
		this.OK.addActionListener(this);
		
		jop1 = new JOptionPane();
		
		
	}
	

	public void actionPerformed(ActionEvent evt) {
			if(evt.getSource() == annuler)
			this.dispose();
			else if(evt.getSource() == OK){
			//jop1.showMessageDialog(null, "Contenu marque "+this.txtMarque.getText());

                            if(testContenu()==true){
                                TraitRequete recupNumType = new TraitRequete();
                                String [] resultatVouluNumType = new String[]{"numType"};
                                String [][] resRecupNumType = recupNumType.ExecuteRequete("SELECT numType FROM typealcool WHERE libelle=\""+this.txtType.getSelectedItem()+"\";", resultatVouluNumType,1);
                                int numType = Integer.parseInt(resRecupNumType[0][0]);
                                
                                TraitRequete recupNumStock = new TraitRequete();
                                String [] resultatVouluNumStock = new String[]{"numStock"};
                                String [][] resRecupNumStock = recupNumStock.ExecuteRequete("SELECT numStock FROM stock WHERE libelleStock=\""+this.txtStock.getSelectedItem()+"\";", resultatVouluNumStock, 1);
                                int numStock = Integer.parseInt(resRecupNumStock[0][0]);
                                
                               
                                String requeteBouteille = "INSERT INTO bouteille VALUES (NULL, "+numType+","+numStock+",'"+this.txtMarque.getText()+"',"+this.txtVolAlcool.getText()+", "+this.txtVolBouteille.getText()+","+this.txtQuantite.getText()+", '"+this.txtCom.getText()+"');";
                                TraitRequete ajoutBouteille = new TraitRequete();
                                ajoutBouteille.ExecuteRequeteData(requeteBouteille);
                                
                                this.dispose();
			}
                        }
	}                

	
	public static void main (String[] a)
	{
		AjoutBouteille bouteille1 = new AjoutBouteille();
	}


public boolean testContenu() {

    if(this.txtMarque.getText().equals("")==false)
    {
        if(this.txtVolAlcool.getText().equals("")==false)
        {
            if(testInt(this.txtVolAlcool.getText())==false)
            {
                jop1.showMessageDialog(null, "Le volume d'Alcool entré est incorrect !");
                return false;
            }
            
            if(this.txtVolBouteille.getText().equals("")==false)
            {
                if(testInt(this.txtVolBouteille.getText())==false)
            {
                jop1.showMessageDialog(null, "Le volume bouteille entré est incorrect !");
                return false;
            }
                if(this.txtType.getSelectedItem()!=null)
                {
                    if(this.txtQuantite.getText().equals("")==false)
                    {
                        if(testInt(this.txtQuantite.getText())==false)
                        {
                            jop1.showMessageDialog(null, "La quantité entrée est incorrecte !");
                            return false;
                        }
                        if(this.txtStock.getSelectedItem()!=null)
                        {
                            return true;
                        }
                    }
                }
            }
        }
    }
    jop1.showMessageDialog(null, "Certains champs ne sont pas remplis !");
    return false;
}  

        
    


public boolean testInt(String chaine) {
    try {
    int i = Integer.parseInt(chaine);
    }
    catch (Exception e) {
    return false;
    }
return true;
}

}

